package com.xu.cloud.common.aop;

import com.xu.cloud.common.api.XuApi;
import com.xu.cloud.common.api.XuApiMethod;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.StaticMethodMatcher;
import org.springframework.core.annotation.AnnotatedElementUtils;

import java.lang.reflect.Method;

/**
 * XuApi切点, 类上有XuApi注解的类, 方法上有XuApiMethod注解
 *
 * @author xuguan
 * @since 2025/1/23
 */
public class XuApiPointcut implements Pointcut {
    @Override
    public ClassFilter getClassFilter() {
        return new ClassFilter() {
            @Override
            public boolean matches(Class<?> cls) {
                return AnnotatedElementUtils.hasAnnotation(cls, XuApi.class);
            }
        };
    }

    @Override
    public MethodMatcher getMethodMatcher() {
        return new StaticMethodMatcher() {
            @Override
            public boolean matches(Method method, Class<?> cls) {
                return AnnotatedElementUtils.hasAnnotation(method, XuApiMethod.class);
            }
        };
    }
}
